package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.BoxBean
import com.zat.mes.andrmes.data.PackBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-12-04
 * @description: 装箱
 */
class PackBoxModel(application: Application) : AndroidViewModel(application){

    private val boxData: MutableLiveData<BoxBean> = MutableLiveData()
    private val postBox:MutableLiveData<BaseResponse<String>> = MutableLiveData()


    fun getBoxData(): LiveData<BoxBean> {
        return boxData
    }
    fun getPostBox():LiveData<BaseResponse<String>>{
        return postBox
    }
    /**
     * 根据卡查询包(做工)
     */
    fun loadGetBox(boxQRCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getBox(boxQRCode)
            .enqueue(object : MesCallBack<BaseResponse<BoxBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<BoxBean>>?,
                    response: Response<BaseResponse<BoxBean>>?
                ) {
                    val result = response?.body()?.result
                    boxData.postValue(result)
                }

            })
    }



    /**
     * 入库
     */
    fun loadSkuBox(jsonArray: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonArray)
        RetrofitManager.apiService(ApiService::class.java)
            .postSkuBox(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postBox.postValue(response?.body())
                }


            })
    }
}